package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.LeaveInfo;
import com.test.dao.LeaveInfoDao;
import com.test.entity.NoticeInfo;
import com.test.service.LeaveInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 请假(LeaveInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("leaveInfoService")
public class LeaveInfoServiceImpl implements LeaveInfoService {
    @Resource
    private LeaveInfoDao leaveInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LeaveInfo queryById(Integer id) {
        return this.leaveInfoDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param leaveInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public ResData queryByPage(LeaveInfo leaveInfo, PageRequest pageRequest) {
        int total = Integer.parseInt(String.valueOf(this.leaveInfoDao.count(leaveInfo)));
        List<LeaveInfo> leaveInfoList = leaveInfoDao.queryAllByLimit(leaveInfo, pageRequest);//查看一共有多少条数据
        int pageSize = pageRequest.getPageSize();//每页多少条
        int countPage = (total%pageSize==0)? (total/pageSize) : ((total/pageSize)+1);//一共有多少页
        List<MyObject> list = leaveInfoList.stream().map(leaveInfo1 -> new MyObject(leaveInfo1)).collect(Collectors.toList());
        return new ResData(ResCode.SUCCESS,list,total,countPage);
    }

    /**
     * 新增数据
     *
     * @param leaveInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(LeaveInfo leaveInfo) {
        return this.leaveInfoDao.insert(leaveInfo);
    }

    /**
     * 修改数据
     *
     * @param leaveInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int update(LeaveInfo leaveInfo) {
        return this.leaveInfoDao.update(leaveInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.leaveInfoDao.deleteById(id) > 0;
    }
}
